<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 10/1/12
 * Time: 5:27 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_Categories extends Page {

	/**
	 * @var Db_Products_Categories
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Products_Categories';
		$this->actionsTableObject = array(
			'saveAction',
			'addEditAction',
			'offersListAction',
			'addCategoryOffersAction',
			'saveCategoryOffersAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		$categories = Db_Products_Categories::getAllCategories();
		self::$DEBUG->addPrivateVar($categories);
		self::$TPL->assign('categories', $categories);
		self::$TPL->setBodyTemplate('list_categories');

		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);
		if(empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
			$projectsByCategoryId=array();
		} else {
			self::$TPL->assign('category', $this->tableObject->objectArray);
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit'), '?action=addEdit&id=' . $this->tableObject->objectArray->id, true);
			$projectsByCategoryId = Db_Products_Categories::getProjectIdsByCategoryId($this->tableObject->id);
			$productsCategory = Db_Products_Products::getProductsCategoryByCategoryId($this->tableObject->objectArray->id);
			self::$TPL->assign('productsCategory', $productsCategory);
		}
		self::$TPL->assign('projectsByCategoryId', $projectsByCategoryId);
		$allProjects = Db_Projects::getAllProjects();
		self::$TPL->assign('allProjects', $allProjects);
		self::$TPL->setBodyTemplate('add_edit_category');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);
		$productCategoryName = post('pc_name', 'str');
		$this->message = t(T_FORM_MSG, 'success');
		if(!empty($productCategoryName)) {
			$this->tableObject->pc_name = $productCategoryName;
			$this->tableObject->pc_name_plural = post('pc_name_plural', 'str');
			$this->tableObject->pc_teaser_title = post('pc_teaser_title', 'str');
			$this->tableObject->pc_teaser_content = post('pc_teaser_content', 'str');
			$this->tableObject->pc_teaser_link_text = post('pc_teaser_link_text', 'str');
			$this->tableObject->pc_default_product_id = post('pc_default_product_id', 'int');
			$this->tableObject->pc_show_envelope = post('pc_show_envelope', 'int');
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			} else {
				if (!Db_Products_Categories::rewriteCategoryProjects($this->tableObject->id, post('cp_project_id', 'array'))) {
					$this->error = true;
					$this->message = t(T_FORM_ERROR, 'duplicate_product');
				}
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		if (!$this->idTableObject) {
			Db_Cms_Pages::updateProductPageUrls();
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Products_Categories::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		Db_Cms_Pages::updateProductPageUrls();
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function offersListAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);

		$this->addBreadcrumb(t(T_CATEGORY_TITLE, 'category_offers') . ' "' . t(T_CATEGORY_TITLE, $this->tableObject->objectArray->pc_name) . '"', '?action=offersList&id=' . $this->tableObject->objectArray->id, true);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addCategoryOffers&id=' . $this->tableObject->objectArray->id);

		$this->message = t(T_FORM_MSG, 'success');
		self::$TPL->appendCssFile('admin/category_offers');
		self::$TPL->appendJsFile('admin/category_offers');

		$categoryId = get('id', 'int');
		$offersList = Db_Products_CategoryOffers::getOffersByCategoryId($categoryId);
		self::$TPL->assign('offersList', $offersList);

		self::$TPL->setBodyTemplate('list_category_offers');
		return array(
			'type' => 'display'
		);
	}

	public function addCategoryOffersAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);

        $parentCategoryName = $this->tableObject->objectArray->pc_name;
		self::$TPL->assign('parentCategoryName', $parentCategoryName);

		$parentCategoryId = get('id', 'int');
		self::$TPL->assign('parentCategoryId', $parentCategoryId);

		$this->addBreadcrumb(t(T_CATEGORY_TITLE, 'category_offers') . ' "' . t(T_CATEGORY_TITLE, $parentCategoryName) . '"', '?action=offersList&id=' . $parentCategoryId);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addCategoryOffers&id=' . $parentCategoryId, true);

		$unusedCategories = Db_Products_Categories::getUnusedCategoriesForOffers($parentCategoryId);
		self::$TPL->assign('unusedCategories', $unusedCategories);
		self::$TPL->setBodyTemplate('add_category_offers');
		return array(
			'type' => 'display'
		);
	}

	public function saveCategoryOffersPositionAction() {
		perm('CATEGORIES_EDIT');
		$btnAction = post('btnAction', 'string');
		$categoryOfferId = post('currentId', 'int');
		$categoryOffer = new Db_Products_CategoryOffers($categoryOfferId);

		$categoryOffer->positionCommonFields = array(
			'co_parent_category_id' => $categoryOffer->objectArray->co_parent_category_id
		);
		$this->moveUpDownAjax($btnAction, $categoryOffer);
	}

	public function saveCategoryOffersAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$parentCategoryId = post('co_parent_category_id', 'int');
		$categoryOffersId = post('co_connected_category_id', 'int');
		$isPersistent = post('co_is_persistent', 'int');
		if (!$parentCategoryId || !$categoryOffersId) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		} else {
			$categoryOffer = new Db_Products_CategoryOffers();
			$categoryOffer->co_parent_category_id = $parentCategoryId;
			$categoryOffer->co_connected_category_id = $categoryOffersId;
			$categoryOffer->co_is_persistent = $isPersistent;

			if ($categoryOffer->save()) {
				$categoryOffer->positionCommonFields = array('co_parent_category_id'=>$parentCategoryId	);
				if(!$categoryOffer->positionSetLast()) {
					$this->error = true;
					$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
				}
			} else {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}
		return array(
			'type' => 'redirect',
			'content' =>'?action=offersList&id=' . $parentCategoryId
		);

	}

	public function deleteCategoryOfferAction() {
		perm('CATEGORIES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$categoryOfferId = get('id', 'int');
		$parentCategoryId = get('parentCategoryId', 'int');
		if (!$categoryOfferId || !$parentCategoryId) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		} else {
			if(!Db_Products_CategoryOffers::deleteByField('id', $categoryOfferId)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
			$categoryOffer = new Db_Products_CategoryOffers($categoryOfferId);
			$categoryOffer->positionCommonFields = array(
				'co_parent_category_id' => $parentCategoryId
			);
			$categoryOffer->positionCleanUp();
			return array(
				'type' => 'redirect',
				'content' =>'?action=offersList&id=' . $parentCategoryId
			);
		}
	}
}